/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CategoryFrm.java
 *
 * Created on 2012-1-5, 14:33:35
 */
package billmanage;

import com.breeze.entity.Tcategory;
import com.breeze.service.ICoreService;
import com.breeze.util.PageList;
import com.breeze.util.SpringUtil;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author huateng
 */
public class CategoryFrm extends javax.swing.JFrame {

    /** Creates new form CategoryFrm */
    public CategoryFrm() {
        initComponents();
        coreService = (ICoreService) SpringUtil.getBean("coreService");
        initTable(null);
        addNextPrevAction();
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                CategoryFrm.this.dispose();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        l_prev = new javax.swing.JLabel();
        l_next = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(billmanage.BillManageApp.class).getContext().getResourceMap(CategoryFrm.class);
        l_prev.setText(resourceMap.getString("l_prev.text")); // NOI18N
        l_prev.setName("l_prev"); // NOI18N

        l_next.setText(resourceMap.getString("l_next.text")); // NOI18N
        l_next.setName("l_next"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(billmanage.BillManageApp.class).getContext().getActionMap(CategoryFrm.class, this);
        jButton1.setAction(actionMap.get("Add")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("Delete")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(l_prev)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(l_next))))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_next)
                    .addComponent(l_prev))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CategoryFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CategoryFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CategoryFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CategoryFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CategoryFrm().setVisible(true);
            }
        });
    }

    @Action
    public void Add() {
        AddCategoryDlg dlg=new AddCategoryDlg(this, true);
        dlg.setLocationRelativeTo(this);
        dlg.setVisible(true);
        pageList.setCurrentPage(1L);
        initTable(null);
    }

    @Action
    public void Delete() {
        int row = jTable1.getSelectedRow();
        Object seqId = jTable1.getModel().getValueAt(row, 0);
        Tcategory category = new Tcategory();
        category.setSeqid((Integer) seqId);
        if (coreService.deleteCategory(category) > 0) {
            JOptionPane.showMessageDialog(this, "delete successful!");
        } else {
            JOptionPane.showMessageDialog(this, "delete failed!");
        }
        initTable(null);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel l_next;
    private javax.swing.JLabel l_prev;
    // End of variables declaration//GEN-END:variables
    private ICoreService coreService;
    private PageList pageList = new PageList();
    private String[] header = new String[]{"id", "display"};

    private void initTable(String op) {
        Tcategory category = new Tcategory();
        getCategoryCount();
        category.setPageList(pageList);
        if ("next".equals(op)) {
            pageList.nextPage();
        } else if ("prev".equals(op)) {
            pageList.prevPage();
        }
        pageList.setResultList(coreService.queryCategoryList(category));

        List list = pageList.getResultList();
        Object[][] cells = new Object[list.size()][3];
        for (int i = 0; i < list.size(); i++) {
            Tcategory u = (Tcategory) list.get(i);
            cells[i][0] = u.getSeqid();
            cells[i][1] = u.getDisplay();
        }

        TableModel tm = new DefaultTableModel(cells, header);
        jTable1.setModel(tm);
    }

    private void addNextPrevAction() {
        //jlabel next and prev
        l_next.addMouseListener(new MouseAdapter() {

            public void mouseReleased(MouseEvent e) {
                initTable("next");
            }
        });
        l_prev.addMouseListener(new MouseAdapter() {

            public void mouseReleased(MouseEvent e) {
                initTable("prev");
            }
        });
    }

    private void getCategoryCount() {
        pageList.setTotalNum(coreService.queryCategoryCount(null));
    }
}
